﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BUS;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using DTO;

namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for TimKiemSachUserControl.xaml
    /// </summary>
    public partial class TimKiemSachUserControl : UserControl
    {
        private Section_DTO Section;
        private MainWindow _parent;
        public TimKiemSachUserControl(Section_DTO sec, MainWindow par)
        {
            _parent = par;
            Section = sec;
            InitializeComponent();
        }

        private void timKiem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataTable data;
                if (rSoLuongTon.IsChecked.Value)
                {
                    int tu, den;
                    if (oTu.Text == "" && oDen.Text == "")
                    {
                        MessageBox.Show("Không có từ khóa để tìm kiếm", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                        oTimKiem.Focus();
                        return;
                    }
                    else
                    {
                        tu = Int32.Parse(oTu.Text);
                        den = Int32.Parse(oDen.Text);
                    }

                    data = Sach_BUS.SACH.TimSachTheoSoLuongTon(tu, den);
                    if (data != null)
                        ketQua.DataContext = data.DefaultView;
                    return;
                }
                else
                {
                    string tukhoa;

                    if (oTimKiem.Text == "")
                    {
                        MessageBox.Show("Không có từ khóa để tìm kiếm", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                        oTimKiem.Focus();
                        return;
                    }
                    else
                    {
                        tukhoa = RemoveUnicode(oTimKiem.Text, true);
                    }




                    if (rMaSach.IsChecked.Value)
                    {
                        data = Sach_BUS.SACH.TimSachTheoMa(Int32.Parse(tukhoa));
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }

                    if (rTenSach.IsChecked.Value)
                    {
                        data = Sach_BUS.SACH.TimSachTheoTen(tukhoa);
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }

                    if (rTheLoai.IsChecked.Value)
                    {
                        data = Sach_BUS.SACH.TimSachTheoTheLoai(tukhoa);
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }

                    if (rTacGia.IsChecked.Value)
                    {
                        data = Sach_BUS.SACH.TimSachTheoTacGia(tukhoa);
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }

                    if (rKhachhang.IsChecked.Value)
                    {
                        data = Sach_BUS.SACH.TimTheoKhachHang(tukhoa);
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }

                    if (rTatca.IsChecked.Value)
                    {
                        int ma;
                        try
                        {
                            ma = Int32.Parse(tukhoa);
                        }
                        catch (Exception)
                        {
                            ma = 0;
                        }
                        Sach_DTO sach = new Sach_DTO(ma, tukhoa, tukhoa, tukhoa, 0);
                        data = Sach_BUS.SACH.TimSachTheoTatCa(sach);
                        if (data != null)
                            ketQua.DataContext = data.DefaultView;
                        return;
                    }
                }
            }
            catch (FormatException ex)
            {
                return;
            }
        }
        
        private void xoa_Click(object sender, RoutedEventArgs e)
        {
            oTimKiem.Text = "";
            oTu.Text = "";
            oDen.Text = "";
            ketQua.DataContext = null;
            rMaSach.IsChecked = true;
            UpdateXoa();
        }

        private void thoat_Click(object sender, RoutedEventArgs e)
        {
            _parent.ChangeUI(new WelcomeUserControl(Section));
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            oTimKiem.Focus();
            rMaSach.IsChecked = true;
        }

        private void rSoLuongTon_Click(object sender, RoutedEventArgs e)
        {
            
            oTu.Text = string.Empty;
            oDen.Text = string.Empty;
            oTimKiem.Text = string.Empty;
            oTu.Focus();
            gTuDen.Visibility = System.Windows.Visibility.Visible;
            oTimKiem.Visibility = System.Windows.Visibility.Hidden;
        }

        private void rSoLuongTon_Unchecked(object sender, RoutedEventArgs e)
        {
            
            oTu.Text = string.Empty;
            oDen.Text = string.Empty;
            oTimKiem.Text = string.Empty;
            oTimKiem.Focus();
            gTuDen.Visibility = System.Windows.Visibility.Hidden;
            oTimKiem.Visibility = System.Windows.Visibility.Visible;
            timKiem.IsEnabled = false;
            xoa.IsEnabled = false;
        }




        #region Ham ho tro
        private string RemoveUnicode(string inputText, bool sqlSearch)
        {
            string stFormD = inputText.Normalize(System.Text.NormalizationForm.FormD);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string str = "";
            for (int i = 0; i <= stFormD.Length - 1; i++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
                if (uc == UnicodeCategory.NonSpacingMark == false)
                {
                    if (stFormD[i] == 'đ')
                        str = "d";
                    else if (stFormD[i] == 'Đ')
                        str = "D";
                    else
                        str = stFormD[i].ToString();

                    if (sqlSearch) str = ReplaceCharSet(str);
                    sb.Append(str);
                }
            }
            return sb.ToString().ToLower();
        }

        private string ReplaceCharSet(string input)
        {
            string charSet = input.ToLower();
            if (charSet == "a")
                return "[aàảãáạăằẳẵắặâầẩẫấậ]";
            else if (charSet == "e")
                return "[eèẻẽéẹêềểễếệ]";
            else if (charSet == "i")
                return "[iìỉĩíị]";
            else if (charSet == "o")
                return "[oòỏõóọôồổỗốộơờởỡớợ]";
            else if (charSet == "u")
                return "[uùủũúụưừửữứự]";
            else if (charSet == "y")
                return "[yỳỷỹýỵ]";
            else if (charSet == "d")
                return "[dđ]";
            return charSet;
        }
        #endregion

        private void oTimKiem_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (oTimKiem.Text == String.Empty)
                timKiem.IsEnabled = false;
            else
                timKiem.IsEnabled = true;

            UpdateXoa();
        }

        private void UpdateXoa()
        {
            if (ketQua.Items.Count == 0 && oTimKiem.Text == String.Empty)
                xoa.IsEnabled = false;
            else
                xoa.IsEnabled = true;
        }

        private void rSoLuongTon_Checked(object sender, RoutedEventArgs e)
        {
            timKiem.IsEnabled = true;
            xoa.IsEnabled = true;
        }

    }
}
